package com.deep.transaction.demo5.advice;

import com.deep.transaction.demo5.util.MyTransactionManager;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionStatus;

/*
注解配置事务通知类
 */

//@Aspect
//@Component
public class TransactionAdvice {
    @Autowired
    private MyTransactionManager transactionManager;

    //@Around("execution(* *..*Service.*(..))")
    public void txAround(ProceedingJoinPoint pjp) throws Throwable {
        //开启事务
        TransactionStatus status = transactionManager.openTransaction();
        try {
            //执行切入点
            pjp.proceed();

            //提交事务
            transactionManager.commit(status);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            //回滚事务
            transactionManager.rollback(status);

        }
    }
}
